package com.ruoyi.firemen.controller;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBElement;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.docx4j.TraversalUtil;
import org.docx4j.finders.ClassFinder;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.ContentAccessor;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.Tc;
import org.docx4j.wml.Tr;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.firemen.domain.FmFiremenInfo;
import com.ruoyi.firemen.service.IFmFiremenInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 人员信息Controller
 * 
 * @author zmc
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/firemen/user")
public class FmFiremenInfoController extends BaseController
{
    @Autowired
    private IFmFiremenInfoService fmFiremenInfoService;

    /**
     * 查询人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('firemen:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(FmFiremenInfo fmFiremenInfo)
    {
        startPage();
        List<FmFiremenInfo> list = fmFiremenInfoService.selectFmFiremenInfoList(fmFiremenInfo);
        return getDataTable(list);
    }

    /**
     * 导出人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('firemen:user:export')")
    @Log(title = "人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FmFiremenInfo fmFiremenInfo)
    {
        List<FmFiremenInfo> list = fmFiremenInfoService.selectFmFiremenInfoList(fmFiremenInfo);
        ExcelUtil<FmFiremenInfo> util = new ExcelUtil<FmFiremenInfo>(FmFiremenInfo.class);
        util.exportExcel(response, list, "人员信息数据");
    }

    /**
     * 获取人员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('firemen:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(fmFiremenInfoService.selectFmFiremenInfoById(id));
    }

    /**
     * 新增人员信息
     */
    @PreAuthorize("@ss.hasPermi('firemen:user:add')")
    @Log(title = "人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FmFiremenInfo fmFiremenInfo)
    {
        fmFiremenInfoService.insertFmFiremenInfo(fmFiremenInfo);
        List<FmFiremenInfo> fmFiremenInfos = fmFiremenInfoService.selectFmFiremenInfoList(fmFiremenInfo);
        return toAjax(fmFiremenInfoService.insertAllTypeForTable(fmFiremenInfos.get(0)));
    }

    /**
     * 修改人员信息
     */
    @PreAuthorize("@ss.hasPermi('firemen:user:edit')")
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FmFiremenInfo fmFiremenInfo)
    {
        return toAjax(fmFiremenInfoService.updateFmFiremenInfo(fmFiremenInfo));
    }

    /**
     * 删除人员信息
     */
    @PreAuthorize("@ss.hasPermi('firemen:user:remove')")
    @Log(title = "人员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(fmFiremenInfoService.deleteFmFiremenInfoByIds(ids));
    }

//    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {


        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(file.getInputStream());
// Load main document part
        MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
        ClassFinder find = new ClassFinder(Tbl.class);
        new TraversalUtil(mainDocumentPart.getContent(), find);

        Tbl table = (Tbl) find.results.get(0);// 获取到第一个表格元素
        List<Object> trs = table.getContent();

        Tc o = (Tc) getAllElementFromObject((Tr) trs.get(0), Tc.class).get(2);
        //姓名
        Object o1 = ((Tc) getAllElementFromObject((Tr) trs.get(0), Tc.class).get(2)).getContent().get(0);
        //出生年月
        Object o2 = ((Tc) getAllElementFromObject((Tr) trs.get(0), Tc.class).get(4)).getContent().get(0);
        //性别
        Object o3 = ((Tc) getAllElementFromObject((Tr) trs.get(1), Tc.class).get(2)).getContent().get(0);
        //文化程度
        Object o4 = ((Tc) getAllElementFromObject((Tr) trs.get(1), Tc.class).get(4)).getContent().get(0);
        //政治面貌
        Object o5 = ((Tc) getAllElementFromObject((Tr) trs.get(2), Tc.class).get(2)).getContent().get(0);
        //身份类别
        Object o6 = ((Tc) getAllElementFromObject((Tr) trs.get(2), Tc.class).get(4)).getContent().get(0);


        System.out.println(o1);
        System.out.println(o2);
        System.out.println(o3);
        System.out.println(o4);
        System.out.println(o5);
        System.out.println(o6);
        FmFiremenInfo fmFiremenInfo = new FmFiremenInfo();
        fmFiremenInfo.setName(o1.toString());
        //        fmFiremenInfo.setDateOfBirth(o2.toString().);
        fmFiremenInfo.setGender(o3.toString());
        fmFiremenInfo.setEducationLevel(o4.toString());
        fmFiremenInfo.setPoliticalStatus(o5.toString());
        fmFiremenInfo.setIdentityType(o6.toString());

        fmFiremenInfoService.insertFmFiremenInfo(fmFiremenInfo);

        String message = "成功";
        return success(message);
    }
    private static List<Object> getAllElementFromObject(Object obj, Class<?> toSearch) {
        List<Object> result = new ArrayList<Object>();
        if (obj instanceof JAXBElement)
            obj = ((JAXBElement<?>) obj).getValue();
        if (obj.getClass().equals(toSearch))
            result.add(obj);
        else if (obj instanceof ContentAccessor) {
            List<?> children = ((ContentAccessor) obj).getContent();
            for (Object child : children) {
                result.addAll(getAllElementFromObject(child, toSearch));
            }
        }
        return result;
    }
}
